import 'package:flutter/material.dart';
import 'package:flutter_spinkit/flutter_spinkit.dart';
import '../../helper/channelHelper.dart';
import '../../model/user.dart';
import '../../widgets/userSelect.dart';

import '../../helper/routeHelper.dart';
import '../../helper/uidata.dart';
import '../../model/danger2.dart';

import '../../widgets/inputWidget.dart';
import '../../widgets/signatureCard2.dart';

import '../../helper/Helper.dart';

class Danger2DealWayPage extends StatefulWidget {
  Danger2DealWayPage(this.cb1, this.added);
  final Danger2 cb1;
  final VoidCallback added;

  @override
  Danger2DealWayPageState createState() => new Danger2DealWayPageState();
}

class Danger2DealWayPageState extends State<Danger2DealWayPage> {
  final String now = Helper.DateTimeNowString();
  final TextEditingController c1 = new TextEditingController();
  bool ifLoad = true;
  late Danger2 s1;
  late User sUser;

  @override
  void initState() {
    super.initState();
    s1 = widget.cb1;
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      //backgroundColor: Colors.white,
      appBar: AppBar(
        title: Text("隐患整改措施"),
      ),

      body: ifLoad
          ? CleanInfoBody(context)
          : Center(
              child: SpinKitCircle(
              color: Colors.blue,
            )),
    );
  }

  CleanInfoBody(BuildContext context) => SingleChildScrollView(
        child: CleanInfoFields(context),
      );

  void dealUser(User u1) {
    sUser = u1;
  }

  List<Widget> getWidgetList(BuildContext context) {
    List<Widget> list1 = <Widget>[];
    List<Widget> list3 = <Widget>[];
    List<Widget> list4 = <Widget>[];
    List<Widget> list2 = <Widget>[];
    List<Widget> list5 = <Widget>[];

    list3.add(InputWidget.getInfo("标题", s1.Title));
    list3.add(InputWidget.getDivider());
    list3.add(InputWidget.getInfo2("内容简述", s1.Content));
    list3.add(InputWidget.getDivider());
    list3.add(InputWidget.getInfo("登记时间", Helper.DateTimeFormat(s1.CreateTM)));

    list4.add(InputWidget.getInput2("整改措施", c1));
    list4.add(InputWidget.getUserSelect("整改责任人", "隐患整改", dealUser));

    list1.add(InputWidget.inCard2(list3));
    list1.add(InputWidget.inCard2(list4));

    list1.add(
        InputWidget.inCard(InputWidget.getButton("提交", () => upload(context))));
    //list1.add(getCLWidget(s1.State==6, Helper.DateTimeFormat(s1.HZQMTM), s1.HZQM));

    return list1;
  }

  upload(BuildContext context) async {
    if (sUser == null) {
      ChannelHelper.showToast("请选择整改责任人！");
      return;
    }

    s1.Deal = c1.text;
    s1.DealTM = Helper.DateTimeNowString();
    s1.DealUser = UIData.CurrentUser!.Guid;
    s1.State = 1;

    await Danger2.Update(s1);
    widget.added();
    Navigator.of(context).pop();
  }

  CleanInfoFields(BuildContext context) => Container(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.end,
          children: getWidgetList(context),
        ),
      );
}
